package com.lw.sbdemo2.service;

import com.github.pagehelper.PageHelper;
import com.lw.sbdemo2.common.base.CrudService;
import com.lw.sbdemo2.common.vo.PageInfo;
import com.lw.sbdemo2.entity.TkExamProductDetail;
import com.lw.sbdemo2.entity.TkTeacherClassStudent;
import com.lw.sbdemo2.entity.TkUserAnswerExamRecord;
import com.lw.sbdemo2.entity.TkUserErrorExamDetail;
import com.lw.sbdemo2.mapper.TkTeacherClassStudentMapper;
import com.lw.sbdemo2.mapper.TkUserErrorExamDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>标题: 用户错题细表服务 - 业务逻辑层</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2019</p>
 * <p>公司: 山人行工作室</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2019-4-14
 */
@Slf4j
@Service
public class TkUserErrorExamDetailService extends CrudService<TkUserErrorExamDetail, TkUserErrorExamDetailMapper> {

    @Autowired
    private TkUserAnswerExamRecordService tkUserAnswerExamRecordService;

    @Autowired
    private TkExamProductDetailService tkExamProductDetailService;

    @Autowired
    private TkTeacherClassStudentService tkTeacherClassStudentService;

    @Autowired
    private TkTeacherClassStudentMapper tkTeacherClassStudentMapper;
	/**
     * 获取用户分页列表
     *
     * @param query    搜索关键词
     * @param pageInfo 分页实体
	 * @param tkUserErrorExamDetail 实体入参
     * @return 用户列表
     */
    public PageInfo<TkUserErrorExamDetail> findListPage(String query, PageInfo pageInfo, TkUserErrorExamDetail tkUserErrorExamDetail) {
        PageHelper.startPage(pageInfo);
        List<TkUserErrorExamDetail> tkUserErrorExamDetaillist = this.dao.findListPage(query, tkUserErrorExamDetail);
        return new PageInfo<>(tkUserErrorExamDetaillist);
    }

    /**
     * 获取班级准确率
     *
     * @param tkUserErrorExamDetail 实体入参
     * @return 用户列表
     */
    public HashMap<Object, Object>  getClassExtract(TkUserErrorExamDetail tkUserErrorExamDetail) throws Exception {

        HashMap<Object, Object> resultMap = new HashMap<>();
        //查询班级错题数情况
        List<TkUserErrorExamDetail> tkUserErrorExamList = this.dao.getClassExtract(tkUserErrorExamDetail);
        //查询试题题目排序
        TkExamProductDetail tkExamProductDetail = TkExamProductDetail.builder().examId(tkUserErrorExamDetail.getExamId()).build();
        List<TkExamProductDetail> examDetailArr = tkExamProductDetailService.findAllList(tkExamProductDetail);

        //获取答题总人数情况
        TkUserAnswerExamRecord tkUserAnswerExamRecord = TkUserAnswerExamRecord.builder()
            .examId(tkUserErrorExamDetail.getExamId()).classId(tkUserErrorExamDetail.getClassId()).build();
        List<TkUserAnswerExamRecord> allList = tkUserAnswerExamRecordService.findAllList(tkUserAnswerExamRecord);

        List<TkTeacherClassStudent> noList = tkTeacherClassStudentMapper.noAnswer(tkUserErrorExamDetail.getExamId(),
                tkUserErrorExamDetail.getClassId());

        resultMap.put("hasList", allList);
        resultMap.put("examName", allList.get(0).getExamName());
        resultMap.put("noList", noList);

        //获得班级总人数
        TkTeacherClassStudent tkTeacherClassStudent = TkTeacherClassStudent.builder().classId(tkUserErrorExamDetail.getClassId()).build();
        Integer studentCount = tkTeacherClassStudentService.getStudentCount(tkTeacherClassStudent);

        //答题总人数
        Integer answerNum = 0;
        if (allList.size() > 0) {
            answerNum = allList.size();
        }

        //答题准率计算
        Integer finalAnswerNum = answerNum;
        List<TkUserErrorExamDetail> tkUserErrorArr =  CollectionUtils.isEmpty(tkUserErrorExamList) ? new ArrayList<>() :
            tkUserErrorExamList.stream().map(item -> {
                Integer questionErrorNum = item.getQuestionErrorNum();
                double a =  new BigDecimal((float) questionErrorNum/finalAnswerNum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                double b = (1 - a)*100;
                if (b > 50) {
                    item.setPercentFlag("#666");
                } else {
                    item.setPercentFlag("red");
                }
                String questionPercent = "0";
                if (b > 10) {
                    questionPercent = (String.valueOf(b)).substring(0 , 2);
                } else {
                    questionPercent = (String.valueOf(b)).substring(0 , 1);
                }

                item.setQuestionPercent(questionPercent);
                return item;
            }).collect(Collectors.toList());

        //按照题库顺序设置题目准确率
        List<TkExamProductDetail> resultArr =
            CollectionUtils.isEmpty(examDetailArr) ? new ArrayList<>() : examDetailArr.stream().map(item->{
                String questionId = item.getQuestionId();
                for (TkUserErrorExamDetail temp : tkUserErrorArr) {
                    if (Objects.equals(questionId,temp.getQuestionId())) {
                        item.setQuestionPercent(temp.getQuestionPercent());
                        item.setPercentFlag(temp.getPercentFlag());
                        break;
                    }
                }
            return item;
            }).collect(Collectors.toList());

        //计算作业准确率
        Integer tkUserErrorCount = 0;
        if (tkUserErrorExamList.size() > 0) {
            tkUserErrorCount = tkUserErrorExamList.size() + 1;
        }
        Integer examCount = 0;
        if (examDetailArr.size() > 0) {
            examCount = examDetailArr.size() + 1;
        }
        double a =  new BigDecimal((float) tkUserErrorCount/examCount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        String examPercent = "0";
        if (a  < 1) {
            examPercent = (String.valueOf((1 - a)*100)).substring(0 , 2);
        }
        //返回回答人数，所有人数
        resultMap.put("answerCount", answerNum);
        resultMap.put("allCount", studentCount);
        resultMap.put("resultArr", resultArr);
        resultMap.put("examPercent", examPercent);

        return resultMap;
    }

}
